package com.qf.zwy.office.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.office.common.base.ex.BaseException;
import com.qf.office.common.base.result.ResultStatus;
import com.qf.office.common.base.utils.CommonBeanutils;
import com.qf.zwy.office.common.qo.AddTrainingQo;
import com.qf.zwy.office.common.qo.TrainingQo;
import com.qf.zwy.office.common.qo.UpdateTrainingQo;
import com.qf.zwy.office.common.vo.ApprovalFormVo;
import com.qf.zwy.office.common.vo.CourseVo;
import com.qf.zwy.office.common.vo.TrainingVo;
import com.qf.zwy.office.entity.Training;
import com.qf.zwy.office.mapper.TrainingMapper;
import com.qf.zwy.office.service.TrainingService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 子未语
 */
@Service
public class TrainingServiceImpl implements TrainingService {

    @Resource
    private TrainingMapper trainingMapper;


    @Override
    public IPage<TrainingVo> selectAll(TrainingQo trainingQo, Integer size, Integer page) {

        // 创建分页查询对象
        IPage<Training> approvalFormPage = new Page<>(page, size);
        // 创建分页查询构造器
        LambdaQueryWrapper<Training> lqw = new LambdaQueryWrapper<>();
        // 添加判断条件
        if (!ObjectUtils.isEmpty(trainingQo.getTrainingStatus()) && trainingQo.getTrainingStatus() != 0) {
            lqw.eq(Training::getTrainingStatus, trainingQo.getTrainingStatus());
        }
        if (!ObjectUtils.isEmpty(trainingQo.getQueryConditions())) {
            lqw.like(Training::getTrainingProgramName, trainingQo.getQueryConditions());
        }
        if (!ObjectUtils.isEmpty(trainingQo.getTrainingClassification()) && trainingQo.getTrainingClassification() != 0) {
            lqw.eq(Training::getTrainingClassification, trainingQo.getTrainingClassification());
        }
        return  CommonBeanutils.convertPageInfo(trainingMapper.selectAllPage(approvalFormPage, lqw), new Page<>(), TrainingVo::new);
    }


    @Override
    public TrainingVo selectOne(Integer id) {
        // 创建条件查询构造器
        LambdaQueryWrapper<Training> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Training::getTrainingId, id);
        return CommonBeanutils.covertTo(trainingMapper.selectOne(lqw), TrainingVo::new);
    }


    @Override
    public Integer add(AddTrainingQo addTrainingQo) {
        return trainingMapper.insert(CommonBeanutils.covertTo(addTrainingQo, Training::new));
    }


    @Override
    public Integer update(UpdateTrainingQo updateTrainingQo) {
        // 判断里面的数据是否为空，为空则不做修改
        if (ObjectUtils.isEmpty(updateTrainingQo)) {
            throw new BaseException(ResultStatus.FAIL_TO_EDIT);
        }
        // 先进行查询是否有这条数据
        Training training = trainingMapper.selectById(updateTrainingQo.getId());
        if (ObjectUtils.isEmpty(training)) {
            throw new BaseException(ResultStatus.USER_NO_EXIST_ERROR);
        }
        // 获取到里面的数据，然后将里面的数据进行判断
        if (!ObjectUtils.isEmpty(updateTrainingQo.getPlannedEndTime())) {
            training.setPlannedEndTime(updateTrainingQo.getPlannedEndTime());
        }
        // 修改里面的状态
        if (!ObjectUtils.isEmpty(updateTrainingQo.getTrainingStatus())) {
            training.setTrainingStatus(updateTrainingQo.getTrainingStatus());
        }
        return trainingMapper.updateById(training);
    }
}
